home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
Resources
/
Chat & Communication
/
Digsby build 37
/
digsby_setup.exe
/
lib
/
M2Crypto
/
X509.pyo
(
.txt
)
< prev
Wrap
Python Compiled Bytecode
|
2008-10-13
|
29KB
|
755 lines
# Source Generated with Decompyle++
# File: in.pyo (Python 2.5)
from M2Crypto import ASN1, BIO, Err, EVP, util
import m2
FORMAT_DER = 0
FORMAT_PEM = 1
class X509Error(Exception):
pass
m2.x509_init(X509Error)
V_OK = m2.X509_V_OK
def new_extension(name, value, critical = 0, _pyfree = 1):
if name == 'subjectKeyIdentifier' and value.strip('0123456789abcdefABCDEF:') is not '':
raise ValueError('value must be precomputed hash')
lhash = m2.x509v3_lhash()
ctx = m2.x509v3_set_conf_lhash(lhash)
x509_ext_ptr = m2.x509v3_ext_conf(lhash, ctx, name, value)
x509_ext = X509_Extension(x509_ext_ptr, _pyfree)
x509_ext.set_critical(critical)
return x509_ext
class X509_Extension:
m2_x509_extension_free = m2.x509_extension_free
def __init__(self, x509_ext_ptr = None, _pyfree = 1):
self.x509_ext = x509_ext_ptr
self._pyfree = _pyfree
def __del__(self):
if getattr(self, '_pyfree', 0) and self.x509_ext:
self.m2_x509_extension_free(self.x509_ext)
def _ptr(self):
return self.x509_ext
def set_critical(self, critical = 1):
return m2.x509_extension_set_critical(self.x509_ext, critical)
def get_critical(self):
return m2.x509_extension_get_critical(self.x509_ext)
def get_name(self):
return m2.x509_extension_get_name(self.x509_ext)
def get_value(self, flag = 0, indent = 0):
buf = BIO.MemoryBuffer()
m2.x509_ext_print(buf.bio_ptr(), self.x509_ext, flag, indent)
return buf.read_all()
class X509_Extension_Stack:
m2_sk_x509_extension_free = m2.sk_x509_extension_free
def __init__(self, stack = None, _pyfree = 0):
if stack is not None:
self.stack = stack
self._pyfree = _pyfree
num = m2.sk_x509_extension_num(self.stack)
for i in range(num):
self.pystack.append(X509_Extension(m2.sk_x509_extension_value(self.stack, i), _pyfree = _pyfree))
else:
self.stack = m2.sk_x509_extension_new_null()
self._pyfree = 1
self.pystack = []
def __del__(self):
if getattr(self, '_pyfree', 0):
self.m2_sk_x509_extension_free(self.stack)
def __len__(self):
return len(self.pystack)
def __getitem__(self, idx):
return self.pystack[idx]
def __iter__(self):
return iter(self.pystack)
def _ptr(self):
return self.stack
def push(self, x509_ext):
self.pystack.append(x509_ext)
ret = m2.sk_x509_extension_push(self.stack, x509_ext._ptr())
return ret
def pop(self):
x509_ext_ptr = m2.sk_x509_extension_pop(self.stack)
if x509_ext_ptr is None:
return None
return self.pystack.pop()
class X509_Name_Entry:
m2_x509_name_entry_free = m2.x509_name_entry_free
def __init__(self, x509_name_entry, _pyfree = 0):
self.x509_name_entry = x509_name_entry
self._pyfree = _pyfree
def __del__(self):
if getattr(self, '_pyfree', 0):
self.m2_x509_name_entry_free(self.x509_name_entry)
def _ptr(self):
return self.x509_name_entry
def set_object(self, asn1obj):
return m2.x509_name_entry_set_object(self.x509_name_entry, asn1obj._ptr())
def set_data(self, data, type = ASN1.MBSTRING_ASC):
return m2.x509_name_entry_set_data(self.x509_name_entry, type, data)
def get_object(self):
return ASN1.ASN1_Object(m2.x509_name_entry_get_object(self.x509_name_entry))
def get_data(self):
return ASN1.ASN1_String(m2.x509_name_entry_get_data(self.x509_name_entry))
def create_by_txt(self, field, type, entry, len):
return m2.x509_name_entry_create_by_txt(self.x509_name_entry._ptr(), field, type, entry, len)
class X509_Name:
nid = {
'C': m2.NID_countryName,
'SP': m2.NID_stateOrProvinceName,
'ST': m2.NID_stateOrProvinceName,
'stateOrProvinceName': m2.NID_stateOrProvinceName,
'L': m2.NID_localityName,
'localityName': m2.NID_localityName,
'O': m2.NID_organizationName,
'organizationName': m2.NID_organizationName,
'OU': m2.NID_organizationalUnitName,
'organizationUnitName': m2.NID_organizationalUnitName,
'CN': m2.NID_commonName,
'commonName': m2.NID_commonName,
'Email': m2.NID_pkcs9_emailAddress,
'emailAddress': m2.NID_pkcs9_emailAddress,
'serialNumber': m2.NID_serialNumber,
'SN': m2.NID_surname,
'surname': m2.NID_surname,
'GN': m2.NID_givenName,
'givenName': m2.NID_givenName }
m2_x509_name_free = m2.x509_name_free
def __init__(self, x509_name = None, _pyfree = 0):
if x509_name is not None:
self.x509_name = x509_name
self._pyfree = _pyfree
else:
self.x509_name = m2.x509_name_new()
self._pyfree = 1
def __del__(self):
if getattr(self, '_pyfree', 0):
self.m2_x509_name_free(self.x509_name)
def __str__(self):
return m2.x509_name_oneline(self.x509_name)
def __getattr__(self, attr):
if attr in self.nid:
return m2.x509_name_by_nid(self.x509_name, self.nid[attr])
if attr in self.__dict__:
return self.__dict__[attr]
raise AttributeError, (self, attr)
def __setattr__(self, attr, value):
if attr in self.nid:
return m2.x509_name_set_by_nid(self.x509_name, self.nid[attr], value)
self.__dict__[attr] = value
def __len__(self):
return m2.x509_name_entry_count(self.x509_name)
def __getitem__(self, idx):
if idx <= idx:
pass
elif not idx < self.entry_count():
raise IndexError('index out of range')
return X509_Name_Entry(m2.x509_name_get_entry(self.x509_name, idx))
def __iter__(self):
for i in xrange(self.entry_count()):
yield self[i]
def _ptr(self):
return self.x509_name
def add_entry_by_txt(self, field, type, entry, len, loc, set):
return m2.x509_name_add_entry_by_txt(self.x509_name, field, type, entry, len, loc, set)
def entry_count(self):
return m2.x509_name_entry_count(self.x509_name)
def get_entries_by_nid(self, nid):
ret = []
lastpos = -1
while True:
lastpos = m2.x509_name_get_index_by_nid(self.x509_name, nid, lastpos)
if lastpos == -1:
break
ret.append(self[lastpos])
return ret
def as_text(self, indent = 0, flags = m2.XN_FLAG_COMPAT):
buf = BIO.MemoryBuffer()
m2.x509_name_print_ex(buf.bio_ptr(), self.x509_name, indent, flags)
return buf.read_all()
def as_der(self):
return m2.x509_name_get_der(self.x509_name)
def as_hash(self):
return m2.x509_name_hash(self.x509_name)
class X509:
m2_x509_free = m2.x509_free
def __init__(self, x509 = None, _pyfree = 0):
if x509 is not None:
self.x509 = x509
self._pyfree = _pyfree
else:
self.x509 = m2.x509_new()
self._pyfree = 1
def __del__(self):
if getattr(self, '_pyfree', 0):
self.m2_x509_free(self.x509)
def _ptr(self):
return self.x509
def as_text(self):
buf = BIO.MemoryBuffer()
m2.x509_print(buf.bio_ptr(), self.x509)
return buf.read_all()
def as_der(self):
return m2.i2d_x509(self.x509)
def as_pem(self):
buf = BIO.MemoryBuffer()
m2.x509_write_pem(buf.bio_ptr(), self.x509)
return buf.read_all()
def save_pem(self, filename):
bio = BIO.openfile(filename, 'wb')
return m2.x509_write_pem(bio.bio_ptr(), self.x509)
def save(self, filename, format = FORMAT_PEM):
bio = BIO.openfile(filename, 'wb')
if format == FORMAT_PEM:
return m2.x509_write_pem(bio.bio_ptr(), self.x509)
elif format == FORMAT_DER:
return m2.i2d_x509_bio(bio.bio_ptr(), self.x509)
else:
raise ValueError('Unknown filetype. Must be either FORMAT_PEM or FORMAT_DER')
def set_version(self, version):
return m2.x509_set_version(self.x509, version)
def set_not_before(self, asn1_utctime):
return m2.x509_set_not_before(self.x509, asn1_utctime._ptr())
def set_not_after(self, asn1_utctime):
return m2.x509_set_not_after(self.x509, asn1_utctime._ptr())
def set_subject_name(self, name):
return m2.x509_set_subject_name(self.x509, name.x509_name)
def set_issuer_name(self, name):
return m2.x509_set_issuer_name(self.x509, name.x509_name)
def get_version(self):
return m2.x509_get_version(self.x509)
def get_serial_number(self):
asn1_integer = m2.x509_get_serial_number(self.x509)
return m2.asn1_integer_get(asn1_integer)
def set_serial_number(self, serial):
asn1_integer = m2.x509_get_serial_number(self.x509)
return m2.asn1_integer_set(asn1_integer, serial)
def get_not_before(self):
return ASN1.ASN1_UTCTIME(m2.x509_get_not_before(self.x509))
def get_not_after(self):
return ASN1.ASN1_UTCTIME(m2.x509_get_not_after(self.x509))
def get_pubkey(self):
return EVP.PKey(m2.x509_get_pubkey(self.x509), _pyfree = 1)
def set_pubkey(self, pkey):
return m2.x509_set_pubkey(self.x509, pkey.pkey)
def get_issuer(self):
return X509_Name(m2.x509_get_issuer_name(self.x509))
def set_issuer(self, name):
return m2.x509_set_issuer_name(self.x509, name.x509_name)
def get_subject(self):
return X509_Name(m2.x509_get_subject_name(self.x509))
def set_subject(self, name):
return m2.x509_set_subject_name(self.x509, name.x509_name)
def add_ext(self, ext):
return m2.x509_add_ext(self.x509, ext.x509_ext, -1)
def get_ext(self, name):
m2x509_get_ext = m2.x509_get_ext
m2x509_extension_get_name = m2.x509_extension_get_name
x509 = self.x509
for i in range(m2.x509_get_ext_count(x509)):
extPtr = m2x509_get_ext(x509, i)
if m2x509_extension_get_name(extPtr) == name:
return X509_Extension(extPtr, _pyfree = 0)
continue
raise LookupError
def get_ext_at(self, index):
if index < 0 or index >= self.get_ext_count():
raise IndexError
return X509_Extension(m2.x509_get_ext(self.x509, index), _pyfree = 0)
def get_ext_count(self):
return m2.x509_get_ext_count(self.x509)
def sign(self, pkey, md):
mda = getattr(m2, md, None)
if mda is None:
raise ValueError, ('unknown message digest', md)
return m2.x509_sign(self.x509, pkey.pkey, mda())
def verify(self, pkey = None):
if pkey:
return m2.x509_verify(self.x509, pkey.pkey)
else:
return m2.x509_verify(self.x509, self.get_pubkey().pkey)
def check_ca(self):
return m2.x509_check_ca(self.x509)
def check_purpose(self, id, ca):
return m2.x509_check_purpose(self.x509, id, ca)
def get_fingerprint(self, md = 'md5'):
der = self.as_der()
md = EVP.MessageDigest(md)
md.update(der)
digest = md.final()
return hex(util.octx_to_num(digest))[2:-1].upper()
def load_cert(file, format = FORMAT_PEM):
bio = BIO.openfile(file)
if format == FORMAT_PEM:
return load_cert_bio(bio)
elif format == FORMAT_DER:
return X509(m2.d2i_x509(bio._ptr()), _pyfree = 1)
else:
raise ValueError('Unknown format. Must be either FORMAT_DER or FORMAT_PEM')
def load_cert_bio(bio, format = FORMAT_PEM):
if format == FORMAT_PEM:
return X509(m2.x509_read_pem(bio._ptr()), 1)
elif format == FORMAT_DER:
return X509(m2.d2i_x509(bio._ptr()), _pyfree = 1)
else:
raise ValueError('Unknown format. Must be either FORMAT_DER or FORMAT_PEM')
def load_cert_string(string, format = FORMAT_PEM):
bio = BIO.MemoryBuffer(string)
return load_cert_bio(bio, format)
def load_cert_der_string(string):
bio = BIO.MemoryBuffer(string)
return X509(m2.d2i_x509(bio._ptr()), _pyfree = 1)
class X509_Store_Context:
m2_x509_store_ctx_free = m2.x509_store_ctx_free
def __init__(self, x509_store_ctx, _pyfree = 0):
self.ctx = x509_store_ctx
self._pyfree = _pyfree
def __del__(self):
if self._pyfree:
self.m2_x509_store_ctx_free(self.ctx)
def _ptr(self):
return self.ctx
def get_current_cert(self):
return X509(m2.x509_store_ctx_get_current_cert(self.ctx), _pyfree = 0)
def get_error(self):
return m2.x509_store_ctx_get_error(self.ctx)
def get_error_depth(self):
return m2.x509_store_ctx_get_error_depth(self.ctx)
def get1_chain(self):
return X509_Stack(m2.x509_store_ctx_get1_chain(self.ctx), 1, 1)
class X509_Store:
m2_x509_store_free = m2.x509_store_free
def __init__(self, store = None, _pyfree = 0):
if store is not None:
self.store = store
self._pyfree = _pyfree
else:
self.store = m2.x509_store_new()
self._pyfree = 1
def __del__(self):
if getattr(self, '_pyfree', 0):
self.m2_x509_store_free(self.store)
def _ptr(self):
return self.store
def load_info(self, file):
m2.x509_store_load_locations(self.store, file)
load_locations = load_info
def add_x509(self, x509):
return m2.x509_store_add_cert(self.store, x509._ptr())
add_cert = add_x509
class X509_Stack:
m2_sk_x509_free = m2.sk_x509_free
def __init__(self, stack = None, _pyfree = 0, _pyfree_x509 = 0):
if stack is not None:
self.stack = stack
self._pyfree = _pyfree
self.pystack = []
num = m2.sk_x509_num(self.stack)
for i in range(num):
self.pystack.append(X509(m2.sk_x509_value(self.stack, i), _pyfree = _pyfree_x509))
else:
self.stack = m2.sk_x509_new_null()
self._pyfree = 1
self.pystack = []
def __del__(self):
if getattr(self, '_pyfree', 0):
self.m2_sk_x509_free(self.stack)
def __len__(self):
return len(self.pystack)
def __getitem__(self, idx):
return self.pystack[idx]
def __iter__(self):
return iter(self.pystack)
def _ptr(self):
return self.stack
def push(self, x509):
self.pystack.append(x509)
ret = m2.sk_x509_push(self.stack, x509._ptr())
return ret
def pop(self):
x509_ptr = m2.sk_x509_pop(self.stack)
if x509_ptr is None:
return None
return self.pystack.pop()
def as_der(self):
return m2.get_der_encoding_stack(self.stack)
def new_stack_from_der(der_string):
stack_ptr = m2.make_stack_from_der_sequence(der_string)
return X509_Stack(stack_ptr, 1, 1)
class Request:
m2_x509_req_free = m2.x509_req_free
def __init__(self, req = None, _pyfree = 0):
if req is not None:
self.req = req
self._pyfree = _pyfree
else:
self.req = m2.x509_req_new()
self._pyfree = 1
def __del__(self):
if getattr(self, '_pyfree', 0):
self.m2_x509_req_free(self.req)
def as_text(self):
buf = BIO.MemoryBuffer()
m2.x509_req_print(buf.bio_ptr(), self.req)
return buf.read_all()
def as_pem(self):
buf = BIO.MemoryBuffer()
m2.x509_req_write_pem(buf.bio_ptr(), self.req)
return buf.read_all()
def as_der(self):
buf = BIO.MemoryBuffer()
m2.i2d_x509_req_bio(buf.bio_ptr(), self.req)
return buf.read_all()
def save_pem(self, filename):
bio = BIO.openfile(filename, 'wb')
return m2.x509_req_write_pem(bio.bio_ptr(), self.req)
def save(self, filename, format = FORMAT_PEM):
bio = BIO.openfile(filename, 'wb')
if format == FORMAT_PEM:
return m2.x509_req_write_pem(bio.bio_ptr(), self.req)
elif format == FORMAT_DER:
return m2.i2d_x509_req_bio(bio.bio_ptr(), self.req)
else:
raise ValueError('Unknown filetype. Must be either FORMAT_DER or FORMAT_PEM')
def get_pubkey(self):
return EVP.PKey(m2.x509_req_get_pubkey(self.req), _pyfree = 1)
def set_pubkey(self, pkey):
return m2.x509_req_set_pubkey(self.req, pkey.pkey)
def get_version(self):
return m2.x509_req_get_version(self.req)
def set_version(self, version):
return m2.x509_req_set_version(self.req, version)
def get_subject(self):
return X509_Name(m2.x509_req_get_subject_name(self.req))
def set_subject_name(self, name):
return m2.x509_req_set_subject_name(self.req, name.x509_name)
def add_extensions(self, ext_stack):
return m2.x509_req_add_extensions(self.req, ext_stack._ptr())
def verify(self, pkey):
return m2.x509_req_verify(self.req, pkey.pkey)
def sign(self, pkey, md):
mda = getattr(m2, md, None)
if mda is None:
raise ValueError, ('unknown message digest', md)
return m2.x509_req_sign(self.req, pkey.pkey, mda())
def load_request(file, format = FORMAT_PEM):
f = BIO.openfile(file)
if format == FORMAT_PEM:
cptr = m2.x509_req_read_pem(f.bio_ptr())
elif format == FORMAT_DER:
cptr = m2.d2i_x509_req(f.bio_ptr())
else:
raise ValueError('Unknown filetype. Must be either FORMAT_PEM or FORMAT_DER')
f.close()
if cptr is None:
raise Err.get_error()
return Request(cptr, 1)
class CRL:
m2_x509_crl_free = m2.x509_crl_free
def __init__(self, crl = None, _pyfree = 0):
if crl is not None:
self.crl = crl
self._pyfree = _pyfree
else:
self.crl = m2.x509_crl_new()
self._pyfree = 1
def __del__(self):
if getattr(self, '_pyfree', 0):
self.m2_x509_crl_free(self.crl)
def as_text(self):
buf = BIO.MemoryBuffer()
m2.x509_crl_print(buf.bio_ptr(), self.crl)
return buf.read_all()
def load_crl(file):
f = BIO.openfile(file)
cptr = m2.x509_crl_read_pem(f.bio_ptr())
f.close()
if cptr is None:
raise Err.get_error()
return CRL(cptr, 1)